# coding: utf-8


class Criteria(dict):
    """
    A callable, JSON-like object that delegate calls to wrapped functions, mostly used in Criteria constructions.
    """

    def __init__(self, func, *args, **kwargs):
        super(Criteria, self).__init__()
        self._criteria = func
        self._args = args
        self._kwargs = kwargs

    def __getattr__(self, item):
        return self.get(item)

    def __call__(self, source_cloud, target_cloud):
        return self._criteria(self, source_cloud, target_cloud, *self._args, **self._kwargs)


def criteria(func):
    """ The criteria decorator.
    It will wrap the given function to be a suitable criteria object.

    :param func: the criteria function to wrap.
    :return: the criteria function.
    """

    def _criteria(*args, **kwargs):
        def _wrap():
            return Criteria(func, *args, **kwargs)

        return _wrap

    return _criteria
